package cn.ctyun.ipush.web.user.service.impl;

import cn.ctyun.ipush.web.user.bo.MenuTreeNode;
import cn.ctyun.ipush.web.user.bo.MenuUserNode;
import cn.ctyun.ipush.web.user.dao.MenuDao;
import cn.ctyun.ipush.web.user.dao.MenuUserDao;
import cn.ctyun.ipush.web.user.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Created by yanghongbo on 2016/7/11.
 */
@Service
public class MenuServiceImpl implements MenuService{
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private MenuUserDao menuUserDao;

    //查询所有菜单项
    public List<MenuTreeNode> findAllMenuTree(MenuTreeNode menuTreeNode){
        return menuDao.listAll(menuTreeNode);
    }

    //查询某一用户的菜单项
    public List<MenuUserNode> findByUser(String userId){

        MenuUserNode menuUserNode = new MenuUserNode();
        menuUserNode.setUserId(userId);

        return menuUserDao.listAll(menuUserNode);
    }

    //增加某一用户添加
    public boolean addMenuUser(String menuTreeNodeId,String userId){

        MenuUserNode menuUserNode = new MenuUserNode();
        menuUserNode.setUserId(userId);
        menuUserNode.setMenuId(menuTreeNodeId);
        List list = menuUserDao.listAll(menuUserNode);
        menuUserNode.setMenuUserNodeId(UUID.randomUUID().toString());

        if(list==null||list.isEmpty()){
            return  menuUserDao.saveMenu(menuUserNode);
        }else{
            return true;
        }

    }
    //delete for 某一用户
    public boolean deleteMenuByUser(String userId){
        List<MenuUserNode> lsit = findByUser(userId);
        try {
            if(lsit!=null && !lsit.isEmpty()){
                for(MenuUserNode e:lsit){
                    if(e!=null){
                        menuUserDao.deleteMenu(e);
                    }
                }
            }
            return true;
        }catch (Exception e){
            return false;
        }

    }

    public boolean addMenu(MenuTreeNode menuTreeNode){
        try{
            menuDao.saveMenu(menuTreeNode);
            return  true;
        }catch (Exception e){
            return false;
        }
    }
    public MenuTreeNode findById(String menuId){
        try{
            MenuTreeNode m = new MenuTreeNode();
            m.setMenuTreeNodeId(menuId);
            return menuDao.querySingleMenu(m);

        }catch (Exception e){
            return null;
        }

    }



}
